package cn.edu.zut.Business;

import cn.edu.zut.Dao.StudentDAO;
import cn.edu.zut.Model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScoreService {
    StudentDAO studentDAO;

    @Autowired
    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public float avarageScore(){
        List<Student>  students = studentDAO.queryAll();
        float scores = 0;
        for(Student s : students){
            scores +=s.getScore();
        }
        System.out.println(scores/students.size());
        return scores/students.size();
    }

    public void insertStudent(Student student){
        studentDAO.insertStudent(student);
    }

    @Transactional
    public void modifyScore(){
        List<Student>  students = studentDAO.queryAll();
        for(Student s: students){
            int score = (int)s.getScore();
            int G=score/10;
            switch (G){
                case 10:
                case 9:s.setScore(1f);break;
                case 8:s.setScore(2f);break;
                case 7:s.setScore(3f);break;
                case 6:s.setScore(4f);break;
                default:s.setScore(5f);
            }
            System.out.println(s.getScore());
            studentDAO.modifyStudent(s);
        }
    }
}
